<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Group extends MY_Controller {
	
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->data['controller'] = 'group';
        $this->load->model('Mgroup');
    }
    
    
    public function index()
	{
	    $this->checkAdminSystem();
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Mgroup->getListGroup($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        // h phan trang cho list.
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên nhóm mạng', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $type = $this->getParamString('type');
            $name = $this->getParamString('name');
            $note = $this->getParamString('notes');
            if($type === 'add'){
                $f = $this->Mgroup->addGroup($name,$note);
                
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Thêm thành công!");window.location="'.$this->data['baseurl'].'group";</script>');
                }else{
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Tên nhóm đã tồn tại!");window.location="'.$this->data['baseurl'].'group";</script>');
                }
            }elseif($type === 'edit'){
                $id = $this->getParamInt('id');
                $f = $this->Mgroup->editGroup($id,$name,$note);
                
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Cập nhật thành công!");window.location="'.$this->data['baseurl'].'group";</script>');
                }else{
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Tên nhóm đã tồn tại!");window.location="'.$this->data['baseurl'].'group";</script>');
                }
            }
            
        }
        
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
    
    public function groupdetail()
    {
        $this->checkAdminSystem();
        $this->data['data'] = $this->Mgroup->getGroupDetail($this->getParamInt('id'));
        $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/group/groupdetail', $this->data);
    }
    
    public function deletegroup()
    {
        $this->checkAdminSystem();
        $f = $this->Mgroup->deleteGroup($this->getParamInt('id'));
        if($f == '200'){
            //die('<meta charset="utf-8"><script type="text/javascript">alert("Xóa thành công!");window.location="'.$this->data['baseurl'].'group";</script>');
            redirect('group');
        }
        
    }
	
}
